mock_instant
mock_instant
This crate allows you to test Instant/Duration code, deterministically per thread.
If cross-thread determinism is required, enable the sync
feature:
= { = "0.3", = ["sync"] }
It provides a replacement std::time::Instant
and std::time::SystemTime
that uses a deterministic thread-local 'clock'
You can swap out the std::time::Instant
with this one by doing something similar to:
use Instant;
use Instant;
or for a std::time::SystemTime
#[cfg(test)]
use mock_instant::{SystemTime, SystemTimeError};
#[cfg(not(test))]
use std::time::{SystemTime, SystemTimeError};
Example
use Duration;
let now = now;
advance;
advance;
// its been '17' seconds
assert_eq!;
Mocking a SystemTime
# use mock_instant::{MockClock, SystemTime};
use std::time::Duration;
let now = SystemTime::now();
MockClock::advance_system_time(Duration::from_secs(15));
MockClock::advance_system_time(Duration::from_secs(2));
// its been '17' seconds
assert_eq!(now.elapsed().unwrap(), Duration::from_secs(17));
Caveats
If the sync
feature is enabled then all tests using this crate will use a global singleton clock.
see https://github.com/museun/mock_instant/issues/6
License: 0BSD